package com.ruoyi.system.service.impl;

import java.util.ArrayList;
import java.util.List;
import com.ruoyi.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.TrainingBaseUserMapper;
import com.ruoyi.system.domain.TrainingBaseUser;
import com.ruoyi.system.service.ITrainingBaseUserService;

import static com.ruoyi.common.utils.SecurityUtils.getUserId;

/**
 * 临床培训基地对应管理员Service业务层处理
 * 
 * @author dp
 * @date 2023-11-30
 */
@Service
public class TrainingBaseUserServiceImpl implements ITrainingBaseUserService 
{
    @Autowired
    private TrainingBaseUserMapper trainingBaseUserMapper;

    /**
     * 查询临床培训基地对应管理员
     * 
     * @param baseUserId 临床培训基地对应管理员主键
     * @return 临床培训基地对应管理员
     */
    @Override
    public TrainingBaseUser selectTrainingBaseUserByBaseUserId(Long baseUserId)
    {
        return trainingBaseUserMapper.selectTrainingBaseUserByBaseUserId(baseUserId);
    }

    /**
     * 查询临床培训基地对应管理员列表
     * 
     * @param trainingBaseUser 临床培训基地对应管理员
     * @return 临床培训基地对应管理员
     */
    @Override
    public List<TrainingBaseUser> selectTrainingBaseUserList(TrainingBaseUser trainingBaseUser)
    {
        return trainingBaseUserMapper.selectTrainingBaseUserList(trainingBaseUser);
    }

    /**
     * 新增临床培训基地对应管理员
     * 
     * @param trainingBaseUser 临床培训基地对应管理员
     * @return 结果
     */
    @Override
    public int insertTrainingBaseUser(TrainingBaseUser trainingBaseUser)
    {
        trainingBaseUser.setCreateTime(DateUtils.getNowDate());
        return trainingBaseUserMapper.insertTrainingBaseUser(trainingBaseUser);
    }

    /**
     * 修改临床培训基地对应管理员
     * 
     * @param trainingBaseUser 临床培训基地对应管理员
     * @return 结果
     */
    @Override
    public int updateTrainingBaseUser(TrainingBaseUser trainingBaseUser)
    {
        return trainingBaseUserMapper.updateTrainingBaseUser(trainingBaseUser);
    }

    /**
     * 批量删除临床培训基地对应管理员
     * 
     * @param baseUserIds 需要删除的临床培训基地对应管理员主键
     * @return 结果
     */
    @Override
    public int deleteTrainingBaseUserByBaseUserIds(Long[] baseUserIds)
    {
        return trainingBaseUserMapper.deleteTrainingBaseUserByBaseUserIds(baseUserIds);
    }

    /**
     * 删除临床培训基地对应管理员信息
     * 
     * @param baseUserId 临床培训基地对应管理员主键
     * @return 结果
     */
    @Override
    public int deleteTrainingBaseUserByBaseUserId(Long baseUserId)
    {
        return trainingBaseUserMapper.deleteTrainingBaseUserByBaseUserId(baseUserId);
    }


    /**
     * 批量新增临床培训基地对应管理员
     *
     * @param baseId 临床基地id
     * @param userIds 需要授权的用户数据ID
     * @return 结果
     */
    @Override
    public int insertAuthUsers(Long baseId, Long[] userIds)
    {
        // 删除对应基地临床培训基地对应管理员信息
        trainingBaseUserMapper.deleteTrainingBaseUserByBaseId(baseId);
        // 新增用户与临床培训基地管理
        List<TrainingBaseUser> list = new ArrayList<TrainingBaseUser>();
        for (Long userId : userIds)
        {
            TrainingBaseUser ur = new TrainingBaseUser();
            ur.setBaseId(baseId.toString());
            ur.setUserId(userId);
            ur.setCreateBy(getUserId().toString());
            ur.setCreateTime(DateUtils.getNowDate());
            list.add(ur);
        }
        return trainingBaseUserMapper.batchUser(list);
    }
}
